using System;
using System.Collections.Generic;
using System.Text;

namespace GURUCORE.Lib.Core
{
    [System.Diagnostics.DebuggerStepThrough()]
    public static class ConvertSafe
    {
        public const decimal FAILCONVERT_DECIMAL = default(decimal);
        public const double FAILCONVERT_DOUBLE = default(double);
        public const int FAILCONVERT_INT = default(int);
        public const string FAILCONVERT_STRING = default(string);
        public const bool FAILCONVERT_BOOL = default(bool);


        #region THACHNN: HELPER Safety Convert Number and String for C1Report


        /// <summary>
        /// Return FAILCONVERT_DECIMAL if input is null, DBNull or string.Empty.
        /// </summary>
        /// <param name="p_objInput"></param>
        /// <returns>FAILCONVERT_DECIMAL if fail</returns>
        public static decimal ToDecimal(object p_objInput)
        {
            decimal decRet = FAILCONVERT_DECIMAL;
            if (null == p_objInput || p_objInput is DBNull)
            {
                return decRet;
            }
            if (p_objInput.ToString() == string.Empty)
            {
                return decRet;
            }
            else
            {
                return Convert.ToDecimal(p_objInput);
            }
        }

        /// <summary>
        /// Return FAILCONVERT_DOUBLE if input is null, DBNull or string.Empty.
        /// </summary>
        /// <param name="p_objInput"></param>
        /// <returns>FAILCONVERT_DOUBLE if fail</returns>
        public static double ToDouble(object p_objInput)
        {
            double dblRet = FAILCONVERT_DOUBLE;
            if (null == p_objInput || p_objInput is DBNull)
            {
                return dblRet;
            }
            if (p_objInput.ToString() == string.Empty)
            {
                return dblRet;
            }
            else
            {
                return Convert.ToDouble(p_objInput);
            }
        }

        /// <summary>
        /// Return FAILCONVERT_INT if input is null, DBNull or string.Empty.
        /// </summary>
        /// <param name="p_objInput"></param>
        /// <returns>FAILCONVERT_INT if false</returns>
        public static int ToInt32(object p_objInput)
        {
            int intRet = FAILCONVERT_INT;
            if (null == p_objInput || p_objInput is DBNull)
            {
                return intRet;
            }
            if (p_objInput.ToString() == string.Empty)
            {
                return intRet;
            }
            else
            {
                try
                {
                    return Convert.ToInt32(p_objInput);
                }
                catch { return intRet; }
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="p_objInput"></param>
        /// <returns>FAILCONVERT_STRING if fail</returns>
        public static string ToString(object p_objInput)
        {
            string strRet = FAILCONVERT_STRING;
            if (null == p_objInput || p_objInput is DBNull)
            {
                return strRet;
            }
            else
            {
                return p_objInput.ToString();
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="p_objInput"></param>
        /// <returns>FAILCONVERT_BOOL if fail</returns>
        public static bool ToBoolean(object p_objInput)
        {
            bool blnRet = FAILCONVERT_BOOL;
            if (null == p_objInput || p_objInput is DBNull)
            {
                return blnRet;
            }
            else
            {
                return Convert.ToBoolean(p_objInput);
            }
        }



        #endregion THACHNN: HELPER Safety Convert Number and String for C1Report
    }
}
